package com.heyqing.topic.RemoveElement_0027;

import java.util.Scanner;

/**
 * ClassName:RemoveElement
 * Package:com.heyqing.topic.RemoveElement_0027
 * Description:
 *
 * @Date:2024/9/9
 * @Author:Heyqing
 */
public class RemoveElement {
    /**
     * 移除元素
     *
     * @param nums
     * @param val
     * @return
     */
    public int removeElement(int[] nums, int val) {
        int prefix = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {
                nums[prefix] = nums[i];
                prefix++;
            }
        }
        return prefix + 1;
    }

    public static void start() {
        RemoveElement removeElement = new RemoveElement();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入val:");
        int val = sc.nextInt();
        System.out.println("请输入nums的长度：");
        int len = sc.nextInt();
        int[] nums = new int[len];
        for (int i = 0; i < len; i++) {
            System.out.print("第" + (i + 1) + "个：");
            nums[i] = sc.nextInt();
        }
        int i = removeElement.removeElement(nums, val);
        System.out.println("去处后的数组：");
        for (int i1 = 0; i1 < i - 1; i1++) {
            System.out.print(nums[i1] + " ");
        }
    }
}
